package lumis.service.pagepersonalization;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.presentation.PortalModeUtil;
import lumis.portal.presentation.perspective.IPortalMode;
import lumis.portal.serviceinterface.IServiceInterfaceRequest;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfiguration;
import lumis.service.pagepersonalization.bean.PagePersonalizationHolderConfiguration.Type;
import lumis.util.ITransaction;


/**
 * Utility class for Personalized Page service.
 * 
 * @version $Revision: 13199 $ $Date: 2011-07-06 11:06:57 -0300 (Wed, 06 Jul 2011) $
 * @since 6.0.0
 */
public class PagePersonalizationUtil
{
	public static final String WIDGETS_PREFERENCES_NAME = "lumPPWidgets";

	public static String[] getDefaultWidgets(SessionConfig sessionConfig, String serviceInterfaceInstanceId, IServiceInterfaceRequest request, ITransaction transaction) throws PortalException
	{
		Type type;
		if (inEditMode(request))
			type = Type.Temporary;
		else
			type = Type.Default;
		
		return getDefaultWidgets(sessionConfig, serviceInterfaceInstanceId, type, transaction);
	}
	
	public static String[] getDefaultWidgets(SessionConfig sessionConfig, String serviceInterfaceInstanceId, Type type, ITransaction transaction) throws PortalException
	{
		PagePersonalizationHolderConfiguration configuration = PagePersonalizationHolderConfiguration.getByTypeAndHolderInstanceId(type, serviceInterfaceInstanceId);
		return configuration != null ? configuration.expand() : new String[] {};
	}

	public static boolean inEditMode(IServiceInterfaceRequest request)
	{
		return PortalModeUtil.getPortalMode(request).getId() == IPortalMode.MODE_PERSONALIZATION;
	}
}
